package com.thz.system.service.video.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thz.system.mapper.report.ReportMapper;
import com.thz.system.mapper.video.VideoMapper;
import com.thz.system.pojo.blog.Blog;
import com.thz.system.pojo.report.Report;
import com.thz.system.pojo.video.Video;
import com.thz.system.service.video.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Description
 * @Author thz
 * @Date 2025/5/11
 */
@Service
public class VideoServiceImpl implements IVideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Value("${files.upload.path:E:\\javaProject\\java\\java\\20250226\\algorithm-oj\\upload\\directory}")
    private String uploadPath;

    @Override
    public List<Video> list(Long courseId) {
        List<Video> videoList = videoMapper.selectList(new LambdaQueryWrapper<Video>()
                .eq(Video::getCourseId, courseId));
        return videoList;
    }

    @Override
    public int uploadVideo(MultipartFile file, String title, String content, Long courseId) {
        try {
            // Create upload directory if it doesn't exist
            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // Generate unique filename
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFilename = UUID.randomUUID().toString() + extension;

            // Save file
            File destFile = new File(uploadPath + File.separator + newFilename);
            file.transferTo(destFile);

            // Create video record
            Video video = new Video();
            video.setTitle(title);
            video.setContent(content);
            video.setCourseId(courseId);
            video.setVideo(newFilename);

            // Save to database
            return videoMapper.insert(video);

        } catch (IOException e) {
            throw new RuntimeException("Failed to upload video", e);
        }
    }

    @Override
    public int update(Long videoId, MultipartFile file, String title, String content) {
        try {
            // Create upload directory if it doesn't exist
            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            // Generate unique filename
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFilename = UUID.randomUUID().toString() + extension;

            // Save file
            File destFile = new File(uploadPath + File.separator + newFilename);
            file.transferTo(destFile);

            // Create video record
            Video video = new Video();
            video.setVideoId(videoId);
            video.setTitle(title);
            video.setContent(content);
            video.setVideo(newFilename);

            // Save to database
            return videoMapper.updateById(video);

        } catch (IOException e) {
            throw new RuntimeException("Failed to upload video", e);
        }
    }

    @Override
    public Video getVideoById(Long videoId) {
        return videoMapper.selectById(videoId);
    }

    @Override
    public int delete(Long videoId) {
        int ret = videoMapper.deleteById(videoId);

        if (ret > 0) {
            deleteUserVideo(videoId);
        }

        return ret;
    }

    private void deleteUserVideo(Long videoId){
//        List<Report> reportList = reportMapper.selectList(new LambdaQueryWrapper<Report>()
//                .select(Report::getReportId)
//                .eq(Report::getVideoId, videoId));
//
//        for (Report report : reportList) {
//            reportMapper.deleteById(report.getReportId());
//        }
        reportMapper.delete(new LambdaQueryWrapper<Report>()
                .eq(Report::getVideoId, videoId));

    }

}
